#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e6 + 10;

char s[N];
int n;
int cnt[500];
int tr[500];

void solve() {
	scanf("%s", s + 1); n = strlen(s + 1);
	cnt['0'] = cnt['6'] = cnt['8'] = cnt['9'] = 0;
	ll ans = 0;
	vec pot = { '0', '6', '8', '9' };
	rep(i, 1, n) {
		cnt[s[i]]++;
		for(auto v : pot) if(v != tr[s[i]]) ans += cnt[v];
		//if(tr[s[i - 1]] == s[i] && tr[s[i]] != s[i]) ans += 1;
		//if(i >= 2 && tr[s[i - 2]] == s[i]) ans += 1;
		//cerr << "!" << ans << endl;
	}
	bool fl = 0;
	rep(i, 1, n) if(tr[s[i]] == s[i]) fl = 1;
	rep(i, 2, n) if(tr[s[i]] == s[i - 1]) fl = 1;
	ans += fl; cout << ans << endl;
}

int main() {
#ifdef YJR_2333_TEST
	freopen("1.in", "r", stdin);
#endif
	tr['0'] = '0', tr['6'] = '9', tr['9'] = '6', tr['8'] = '8';
	for(int T = in; T; T--) solve(); return 0;
}
